package com.suwish.pc.ui.main;

import static com.suwish.pc.util.ResourceUtils._Icon;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.event.ActionEvent;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;

import javax.swing.AbstractAction;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.JToolBar;

import com.android.ddmlib.AndroidDebugBridge.IDeviceChangeListener;
import com.android.ddmlib.IDevice;
import com.suwish.pc.Setting;
import com.suwish.pc.phone.PhoneManager;
import com.suwish.pc.ui.MainFrameToolBar;
import com.suwish.pc.ui.MainStatusBar;
import com.suwish.pc.ui.component.UIFrame;
import com.suwish.pc.ui.component.tab.UITabPanel;
import com.suwish.pc.ui.component.tab.UITabbedPanel;
import com.suwish.pc.ui.dialog.DdmsDeviceSelectDialog;
import com.suwish.pc.ui.dialog.ShellDialog;
import com.suwish.pc.ui.tab.UITabFactory;
import com.suwish.pc.ui.util.DeviceHelper;
import com.suwish.pc.ui.util.GraphicUtils;
import com.suwish.pc.ui.util.UIHelper;
import com.suwish.pc.util.StringUtils;
/**
 * 
 * @author Min
 *
 */
public class MainFrame extends UIFrame{

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	
	private static MainFrame instance = null;
	
	private JMenuBar menuBar = null;
	private JMenu fileMenu = null;
	private JMenu viewMenu = null;
	private JMenu toolMenu = null;
	private JMenu helpMenu = null;
	
	private MainFrameToolBar toolbar = null;
	
	private UITabbedPanel mainTabPane = null;
	
	private MainStatusBar statusBar = null;
	
	/**
	 * 
	 */
	public MainFrame(){
		setTitle("Java Phone Bridge");
		setDefaultCloseOperation(JFrame.DO_NOTHING_ON_CLOSE);
		initUI();
		initData();
		initEvent();
		setSize(new Dimension(1024, 700));
		setMinimumSize(new Dimension(1024, 700));
		GraphicUtils.centerWindowOnScreen(this);
		setVisible(true);
		instance = this;
	}
	/**
	 * 单例
	 * 
	 * 
	 * @return
	 */
	public static MainFrame getInstance(){
		return instance;
	}

	private void initData(){
		
	}
	private void initEvent(){
		PhoneManager.getInstance().getDdmsManager().addDeviceChangeListener(new IDeviceChangeListener() {

			@Override
			public void deviceConnected(IDevice device) {}

			@Override
			public void deviceDisconnected(IDevice device) {
				int index = mainTabPane.getTabIndex(device.getSerialNumber());
				if(index == -1) return;
				mainTabPane.removeTabAt(index);
				UIHelper.showMessageDialog(DeviceHelper.getDeviceDescName(device) + " disconnect...");
			}

			@Override
			public void deviceChanged(IDevice device, int changeMask) {}
			
		});
		addWindowListener(new WindowAdapter() {			
			@Override
			public void windowClosing(WindowEvent e) {
				switch(StringUtils.parseInt(Setting.get("window.close"))){
				case 1 : PhoneManager.getInstance().exitSystem(); break;
				case 2 : getInstance().setState(ICONIFIED);break;
				default :  PhoneManager.getInstance().exitSystem();
				}
			}

			@Override
			public void windowClosed(WindowEvent e) {
//				System.out.println("windowClosed");
			}			
		});
	}
	private void initUI(){
		initMenu();
		getIContentPane().setLayout(new BorderLayout());
		toolbar = new MainFrameToolBar(this);
		toolbar.setFloatable(false);
//		toolbar.setBackground(Color.GREEN);
//		JToolBar bar = new JToolBar();
		getIContentPane().add(toolbar, BorderLayout.NORTH);
		mainTabPane = new UITabbedPanel();
		
//		mainTabPane.addT
		getIContentPane().add(mainTabPane, BorderLayout.CENTER);
		statusBar = new MainStatusBar();
		getIContentPane().add(statusBar, BorderLayout.SOUTH);
		
		setIconImage(_Icon("Gnome-App-icon_16x16.png").getImage());
	}
	
	private void initMenu(){
		menuBar = new JMenuBar();
		setJMenuBar(menuBar);
		fileMenu = new JMenu("File");
		menuBar.add(fileMenu);
		JMenuItem install = new JMenuItem("Install APK");		
		fileMenu.add(install);
		install.addActionListener(new AbstractAction() {
			private static final long serialVersionUID = 1L;
			@Override
			public void actionPerformed(ActionEvent e) {				
				try{
					DeviceHelper.installAPK();
				}catch(Exception ex){
					ex.printStackTrace();
				}
			}});
		JMenuItem installAdvance = new JMenuItem("Install APK ...");
		installAdvance.addActionListener(new AbstractAction() {
			private static final long serialVersionUID = 1L;
			@Override
			public void actionPerformed(ActionEvent e) {
				// TODO Auto-generated method stub
				
			}
		});
		fileMenu.add(installAdvance);
		installAdvance.setEnabled(false);
		fileMenu.addSeparator();
		JMenuItem exitItem = new JMenuItem("Exit", _Icon("application-exit_16x16.png"));
		fileMenu.add(exitItem);
		exitItem.addActionListener(new AbstractAction() {	
			private static final long serialVersionUID = 1L;
			@Override
			public void actionPerformed(ActionEvent e) {
				PhoneManager.getInstance().exitSystem();				
			}
		});
		viewMenu = new JMenu("View");
		viewMenu.add(UIHelper.getLookAndFeelMenu());
		menuBar.add(viewMenu);
		
		toolMenu = new JMenu("Tool");
		JMenuItem connectItem = new JMenuItem("Connect...", _Icon("gtk-new_16x16.png"));
		toolMenu.add(connectItem);
		connectItem.addActionListener(new AbstractAction() {
			private static final long serialVersionUID = 1L;
			@Override
			public void actionPerformed(ActionEvent e) {
				DdmsDeviceSelectDialog dialog = new DdmsDeviceSelectDialog(getInstance());	
				dialog.setVisible(true);
				IDevice device = dialog.getSelectedDevice();
				if(device == null)return;
				addTabCard(device);
			}
		});
		JMenuItem apkViewItem = new JMenuItem("Apk View");
		apkViewItem.addActionListener(new AbstractAction() {
			private static final long serialVersionUID = 1L;
			@Override
			public void actionPerformed(ActionEvent e) {
				UIHelper.viewApkDetail(getInstance());
				
			}
		});
		toolMenu.add(apkViewItem);
		toolMenu.addSeparator();
		JMenuItem shellItem = new JMenuItem("Shell", _Icon("Gnome-Utilities-Terminal_16x16.png"));
		shellItem.addActionListener(new AbstractAction() {	
			private static final long serialVersionUID = 1L;

			@Override
			public void actionPerformed(ActionEvent e) {
				new ShellDialog(getInstance(), "Shell - command line").setVisible(true);
			}
		});
		toolMenu.add(shellItem);
		JMenuItem preferenceItem = new JMenuItem("Preferences", _Icon("Gnome-Preferences-System_16x16.png"));
		preferenceItem.addActionListener(new AbstractAction() {
			private static final long serialVersionUID = 1L;
			@Override
			public void actionPerformed(ActionEvent e) {
				PhoneManager.getInstance().getPerferenceManager().showDialog();
			}
		});
		toolMenu.add(preferenceItem);
		menuBar.add(toolMenu);
		helpMenu = new JMenu("Help");		
		menuBar.add(helpMenu);
		JMenuItem supportItem = new JMenuItem("Support", _Icon("Gnome-Web-Browser_16x16.png"));
		supportItem.setToolTipText("Support online");
		supportItem.addActionListener(new AbstractAction() {
			private static final long serialVersionUID = 1L;
			@Override
			public void actionPerformed(ActionEvent e) {
				UIHelper.openSupportWebsite();				
			}
		});
		helpMenu.add(supportItem);
		JMenuItem versionItem = new JMenuItem("Version");
		helpMenu.add(versionItem);
		versionItem.addActionListener(new AbstractAction() {
			private static final long serialVersionUID = 1L;
			@Override
			public void actionPerformed(ActionEvent e) {
				UIHelper.showMessageDialog(getInstance(), Setting.get("version"), "Message", 1);
				
			}
		});
	}
	
	
	public void addTabCard(IDevice device){
		int index = mainTabPane.getTabIndex(device.getSerialNumber());
		if(index != -1){
			mainTabPane.setSelectedIndex(index);
			return;
		}
		mainTabPane.addContainerComponent(UITabFactory.createTabByDevice(device));
	}
	public void addTabCard(UITabPanel tabPanel){
		
	}
	/**
	 * 
	 * @return
	 */
	public UITabbedPanel getTabbedPane(){
		return mainTabPane;
	}
	/**
	 * 
	 * @return
	 */
	public MainStatusBar getStatusBar(){
		return statusBar;
	}
	
	public MainFrameToolBar getToolBar(){
		return toolbar;
	}
	
}
